//package com.lry.basic.sync;
//
//
//import org.openjdk.jol.info.ClassLayout;
//import org.openjdk.jol.vm.VM;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.CountDownLatch;
//
//
///**
// * @author:刘仁有
// * @desc:
// * @email:953506233@qq.com
// * @data:2019/7/16
// */
//
////大致是说如果启用偏向锁的话，偏向锁会有几秒的延迟，这是为了在启动JVM时候降低资源消耗的一种措施
////摘录自openJDK源码
////biasedLocking.cpp
////        void BiasedLocking::init() {
////        if (UseBiasedLocking) {
////             if (BiasedLockingStartupDelay > 0) {
////                     EnableBiasedLockingTask* task = new EnableBiasedLockingTask(BiasedLockingStartupDelay);
////                     task->enroll();
////             } else {
////                    VM_EnableBiasedLocking op(false);
////                    VMThread::execute(&op);
////             }
////           }
////        }
////markOop.hpp
////  32 bits:
////  --------
////  hash:25 ------------>| age:4    biased_lock:1 lock:2 (normal object)
////  JavaThread*:23 epoch:2 age:4    biased_lock:1 lock:2 (biased object)
////  size:32 ------------------------------------------>| (CMS free block)
////  PromotedObject*:29 ---------->| promo_bits:3 ----->| (CMS promoted object)
////
////  64 bits:
////  --------
////  unused:25 hash:31 -->| unused:1   age:4    biased_lock:1 lock:2 (normal object)
////  JavaThread*:54 epoch:2 unused:1   age:4    biased_lock:1 lock:2 (biased object)
////  PromotedObject*:61 --------------------->| promo_bits:3 ----->| (CMS promoted object)
////  size:64 ----------------------------------------------------->| (CMS free block)
////    对象：
////1:对象头object header（12字节）
////            Common structure at the beginning of every GC-managed heap object.
////            (Every oop points to an object header.)
////            Includes fundamental information about the heap
////            objects layout, type, GC state, synchronization state, and identity hash code.
////            Consists of two words. In arrays it is immediately followed
////            by a length field. Note that both Java objects and
////            VM-internal objects have a common object header format.
////
////            1.1 markword：（8字节）
////            The first word of every object header.
////            Usually a set of bitfields including synchronization state
////            and identity hash code. May also be a pointer
////            (with characteristic low bit encoding) to
////            synchronization related information.
////            During GC, may contain GC state bits.
////            1.2 klass pointer（4字节）类型指针，指向类元数据，JVM通过这个指针确定该对象是哪个类的实例
////            The second word of every object header.
////            Points to another object (a metaobject)
////            which describes the layout and behavior
////            of the original object. For Java objects,
////            the "klass" contains a C++ style "vtable".
////
////2:实例数据boolean flag
////3:对齐填充（为了保证对象字节是8的整数倍）
////  0  01无锁
////  1 01偏向
////    00轻量
////    10重量
////    11GC
////    主要研究markword这8个字节
////-XX:+UseBiasedLocking -XX:BiasedLockingStartupDelay=0
////    17747ms
//    //1352ms
////         System.out.println("jvm‐‐‐‐‐‐‐‐‐‐‐‐"+Integer.toHexString(a.hashCode()));
////    System.out.println("before lock:"+ClassLayout.parseInstance(a).toPrintable());
//public class Sync1 {
//    public static void main(String[] args) throws Exception {
//        final List<A> list = new ArrayList<>();
//
//        //偏->轻
//        //CAS
//        //旧  预期  新
//        //旧的mark word   无锁态   新
//        Thread t = new Thread(){
//            @Override
//            public void run() {
//                for(int i=0;i<25;i++){
//                    A a = new A();
//                    synchronized (a){//偏向锁，偏心于t这个线程
//                        list.add(a);
//                    }
//                }
//            }
//        };
//        t.start();
//        t.join();
//        System.out.println("偏锁:"+ClassLayout.parseInstance(list.get(0)).toPrintable());//偏
//        //<20 无竞争，线程交替执行 ，轻锁
//        //>=20 批量偏向
//        new Thread(){
//            @Override
//            public void run() {
//                for(int i=0;i<list.size();i++){
//                    A a = list.get(i);
//                    synchronized (a){
//                        if(i<19)
//                        System.out.println("轻锁:"+ClassLayout.parseInstance(a).toPrintable());
//                        if(i>=19)
//                        System.out.println("批量偏向:"+ClassLayout.parseInstance(a).toPrintable());
//                    }
//                }
//            }
//        }.start();
//    }
//}
//
//
